package cn.edu.hstc.wm.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import cn.edu.hstc.wm.AppApplication;
import cn.edu.hstc.wm.Constants;
import cn.edu.hstc.wm.utils.IntentUtil;
import cn.edu.hstc.wm.utils.LogUtil;
import cn.edu.hstc.wm.widget.CustomToast;

/**
 * 界面工具类
 */
public class UIHelper {

	/**
	 * 显示网络设置的对话框
	 * 
	 * @param context
	 *            调用该对话框的Activity
	 */
	public static void showNetWorkSetup(final Activity context) {
		// 提示对话框
		AlertDialog.Builder builder = new Builder(context);
		builder.setTitle("网络设置提示").setMessage("网络连接不可用,是否进行设置?");
		builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = null;
				// 判断手机系统的版本 即API大于10 就是3.0或以上版本
				if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
					intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
				} else {
					intent = new Intent();
					ComponentName component = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
					intent.setComponent(component);
					intent.setAction("android.intent.action.VIEW");
				}
				context.startActivityForResult(intent, Constants.SETUP_NETWORK_REQUEST_CODE);
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				dialog.dismiss();
			}
		});
		Dialog dialog = builder.create();
		dialog.setCanceledOnTouchOutside(true);
		// dialog.setFeatureDrawableAlpha(0, 100);
		dialog.show();
	}

	/**
	 * 显示用户登录窗口
	 * 
	 * @param context
	 */
	public static void showLoginDialog(Activity context, int type) {

		// IntentUtil.startActivity(context, UserLoginDialog.class);
		Intent intent = new Intent(context, UserLoginDialog.class);
		intent.putExtra(Constants.LOGIN_TYPE, type);
		context.startActivity(intent);
	}

	/**
	 * 登录/注销
	 * 
	 * @param context
	 */
	public static void showLoginOrLogoutDialog(final Activity context, int type) {
		final AppApplication app = (AppApplication) context.getApplicationContext();
		// 已经登录
		if (app.isLogin()) {
			AlertDialog.Builder builder = new Builder(context);
			builder.setTitle("注销登录").setMessage("真的要注销登录?").setPositiveButton("是的", new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					app.logout();
					CustomToast.make(context).setText("已退出登录").show();
				}
			}).setNegativeButton("不", new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			}).create().show();
		} else {
			// 显示登录窗口
			showLoginDialog(context, type);
		}
	}

	/**
	 * 显示我的评论
	 * 
	 * @param context
	 */
	public static void showMyComment(Activity context, int type) {
		AppApplication app = (AppApplication) context.getApplicationContext();
		if (app.isLogin()) {
			IntentUtil.startActivity(context, MyComment.class);
		} else {
			showLoginDialog(context, type);
		}
	}

	/**
	 * 显示我的订单
	 * 
	 * @param context
	 */
	public static void showMyOrder(Activity context, int type) {
		AppApplication app = (AppApplication) context.getApplicationContext();
		if (app.isLogin()) {
			IntentUtil.startActivity(context, MyOrder.class);
		} else {
			showLoginDialog(context, type);
		}
	}

	public static void showUserInfo(Activity context, int type) {
		AppApplication app = (AppApplication) context.getApplicationContext();

		if (app.isLogin()) {
			// 已登录
			IntentUtil.startActivity((Activity) context, UCenter.class);
		}
		// 未登录
		else {
			showLoginDialog(context, type);
		}
	}

	/**
	 * 显示更新日志窗口
	 * 
	 * @param context
	 */
	public static void showChangeLog(Context context) {
		ChangeLogDialog dialog = ChangeLogDialog.getInstance(context);
		dialog.setStyle("h1 { margin-left: 10px; font-size: 12pt; color: #006b9a; margin-bottom: 0px;}" + "li { margin-left: 0px; font-size: 12pt; padding-top: 10px; }"
				+ "ul { padding-left: 30px; margin-top: 0px; }" + ".summary { margin-left: 10px; font-size: 10pt; color: #006b9a; margin-top: 5px; display: block; clear: left; }"
				+ ".date { margin-left: 10px; font-size: 10pt; color: #006b9a; margin-top: 5px; display: block; }");
		dialog.show();
	}

	/**
	 * 去下單
	 * @param context
	 * @param bundle
	 */
	public static void goOrder(Activity context, Bundle bundle) {
		AppApplication app = (AppApplication) context.getApplication();
		Intent intent ;
		if (app.isLogin()) {
			intent = new Intent(context,GoOrder.class);
			intent.putExtras(bundle);
			context.startActivityForResult(intent, 0);
		} else {
			intent= new Intent(context, UserLoginDialog.class);
			//携带数据传输到登錄界面
			intent.putExtras(bundle);
			intent.putExtra(Constants.LOGIN_TYPE, Constants.LOGIN_TYPE_GO_ORDER);
			context.startActivity(intent);
		}
	}

}
